//日期计算天数小时

export function formatDate(date) {
	console.log(date)
	var date1=date.replace("-","/")
	//var date1 = date;
	let time1 = new Date(date1);
	let time2 = new Date();
	// 计算相差的毫秒数
	let diff = Math.abs(time1 - time2);
    console.log(time1+"：对应："+time2)
	// 将毫秒数转换为天数
	let days = Math.floor(diff / (1000 * 60 * 60 * 24));
	if (days > 7) {
		if (date.split(' ').length > 0) {
			return date.split(' ')[0]
		}
		return date;
	}
	if (days >= 1 && days <= 7) {
		return days + "天前"
	}
	// 将毫秒数转换为小时
	let hours = Math.floor(diff / (1000 * 60 * 60));
	if (hours >= 1) {
		return hours + "小时前"
	}
	// 将毫秒数转换为分钟
	let mins = Math.floor(diff / (1000 * 60));
	if (mins > 1) {
		return mins + "分钟前"
	}
	return "刚刚";
}

/**
 * 获取任意时间
 */
export function getDate(date, AddDayCount = 0) {
	if (!date) {
		date = new Date()
	}
	if (typeof date !== 'object') {
		date = date.replace(/-/g, '/')
	}
	const dd = new Date(date)

	dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

	const y = dd.getFullYear()
	const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
	const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
	return {
		fullDate: y + '-' + m + '-' + d,
		year: y,
		month: m,
		date: d,
		day: dd.getDay()
	}
}

/**
 * 获取任意时间
 */
export function getDatePlusMonth(date, addMonth = 0) {
	if (!date) {
		date = new Date()
	}
	if (typeof date !== 'object') {
		date = date.replace(/-/g, '/')
	}
	const dd = new Date(date)

	dd.setMonth(dd.getMonth()+ addMonth) // 获取AddDayCount天后的日期

	const y = dd.getFullYear()
	const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
	const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
	return {
		fullDate: y + '-' + m + '-' + d,
		year: y,
		month: m,
		date: d,
		day: dd.getDay()
	}
}